--
-- Author: songjianbang
-- Date: 2018-8-14 
--
-- RewardNodeRender

local RewardView = require("app.system.secret.SecretRewardView")

local gRes = Res
local Res = {
    RewardNode = "ui2/activity/Node_reward.csb",
}

setmetatable(Res, {__index = gRes})

local ClassRef = app.mvc.createRenderClass(...)

--data = {count=, rewardID=, rewardIcon=, finished=, rewarded=}
function ClassRef.create(data, node)
	local sprite = ClassRef.extend(node or cc.CSLoader:createNode(Res.RewardNode))
	if node == nil then
		ccui.Helper:doLayout(sprite)
	end
	sprite:init(data)
	return sprite
end       

function ClassRef:init(data)
    self.super.init(self, data)
    
	self.icon = self:findChild("Panel/icon")
	self.iconMask = self:findChild("Panel/mask")
	
	self:initClickEvent()
	self:refresh()
end

function ClassRef:onGetRewardClick(cb)
	self.getRewardCb = cb
end

function ClassRef:initClickEvent()
	local panel = self:findChild("Panel")
	panel:addTouchEventListener(function(sender, eventType)
		if eventType == ccui.TouchEventType.ended then
			local cfg = self._data.config
			if cfg then
				local finished = self._data.finished
				local rewarded = self._data.rewarded
				local sidx = string.find(cfg.rewardID, ";")
				if finished and not rewarded then						
					if sidx then
						RewardView.showRewardView({title=L("lua_code_text_46"), buttonName=L("lua_code_text_59"), drops=cfg.rewardID}, function()
							if self.getRewardCb then
								self.getRewardCb(checknumber(cfg.ID))
							end
						end)
					else
						if self.getRewardCb then
							self.getRewardCb(checknumber(cfg.ID))
						end
					end
				else
					if sidx then
						RewardView.showRewardView({title=L("lua_code_text_46"), buttonName=L("lua_code_text_59"), drops=cfg.rewardID})
					else
						local dl = UD:parseDropStrToBagItem(cfg.rewardID)
						UIHandler:showItemTips(dl[1], panel)
					end
				end
			end
				
		end
	end )
end

function ClassRef:refresh()
	if self._data then
		local data = self._data

		if data.config and data.config.rewardiconName then
			-- print("art/activity_icon/"..data.config.rewardiconName..".png")
			self.icon:loadTexture("art/activity_icon/"..UD:parseClassText(data.config.rewardiconName)..".png")
			self.iconMask:loadTexture("art/activity_icon/"..UD:parseClassText(data.config.rewardiconName)..".png")
		end
	
		self:findChild("Panel/Panel_num/num"):setString(tostring(data.count))
	
		self:findChild("Panel/mask"):setVisible(data.rewarded or not data.finished)
		self:findChild("Panel/icon_over"):setVisible(data.rewarded)

		self:findChild("Panel/ani_img"):setVisible(data.finished and not data.rewarded)
		self:findChild("Panel/tishi"):setVisible(data.finished and not data.rewarded)
	end
end

function ClassRef:onBtnClick(cb)
	self._onBtnClick = cb
end

return ClassRef